<?php
require 'include.php';
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
if(!$action){
	entete('accueil');
	echo "<div id = 'action'>\n";	
} 
	$membre_id = check_cookie();
	if((!$action) || $action == 'index'){
		$membre = new membre($membre_id);
		$cats = array(
					  "accueil" => 'accueil',
					  "infos" => 'infos',  
					  "&eacute;quipes" => "equipes",
					  "recheche" => "recherche"
					);
		if(check_cookie()){
			$cats['ma page'] = "ma_page";	
			echo "<p class = 'center'>Bienvenue $membre->prenom</p>\n";
		} 
		else echo "<p class = 'center'>Bienvenue invit&eacute;</p>\n";
		foreach($cats as $cat => $link){
			echo "<p class = 'center'><button type = 'button' onclick = '$link()' style = 'width: 200px'>$cat</button></p>\n";
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'self.location.href = \"http://www.vbcll.ch/index.php?from_iphone=1\"' style = 'width: 200px'>site complet</button></p>\n";
		$q = "select id,date_format(ddn,'d.m') from membres where date_format('d.m',ddn) = date_format('d.m',curdate());";
		$r = ExecRequete($q);
		$annis = array();
		while($s = LigneSuivante($r)){
			$anni = new membre($s->id);
			$annis[] = $anni->prenom." ".$anni->nom." (".implode(", ",$anni->equipes).")"; 
		}
		if(count($annis) && $membre_id){
			echo "<div class = 'separation'>\n";
			echo "<p><span class = 'non'>Anniversaires</span> : ".implode(" ; ",$annis)."</p>\n";
			echo "</div>\n";			
		}
	}
	elseif($action == 'accueil' || !$membre_id){
		$q = "select infos.id as id,infos.titre as titre, datetime, concat(prenom,' ',nom) as auteur from infos left join membres on (membre_id = membres.id) where infos.valide = 1 order by datetime desc limit 1";
		$r = ExecRequete($q);
		if($s = LigneSuivante($r)){
			echo "<div class = 'separation'>\n";
			echo "<p onclick = 'infos($s->id)' style = 'width: 69%;float: left;'><span class = 'bold'>$s->titre</span><br />\n";
			echo "<span class = 'small fade'>[".format_date($s->datetime,"d.m.Y")."] $s->auteur</span>";
			echo "</p>\n";
			echo "<p style = 'width: 29%; float: left; text-align: center;'><button type = 'button' onclick = 'infos()' class = 'small'>infos</button></p>\n";			
			echo "<br class = 'clear' />\n";
			echo "</div>\n";
		}

		$equipes = liste('equipes');
		$matchs = array();
		foreach($equipes as $id => $nom){
			${"match_".$id} = new match('last','',$nom);
			if((${"match_".$id}->no)) $matchs[${"match_".$id}->date.${"match_".$id}->no] = ${"match_".$id};
		}
		krsort($matchs);
		$i = 0;
		$fields = array("equipe" => '',"match" => "","resultat" => 'center');
		$table = new table('resultats');
		foreach($fields as $field => $class){
			$$field = new table_field($field,'resultats');
			$$field->print = $field;
			$$field->styleclass = $class;
			$table->fields[] = $$field;
		}
		foreach($matchs as $key => $match){
			$equipe = new equipe($match->equipe,"nom");
			$row = new table_row();
			foreach($fields as $field => $class){
				if($field == 'equipe'){
					$equipe_id = array_search($match->equipe,$equipes);
					$print = "<a href = 'javascript:equipes(\"$equipe->id\",\"resume\",\"accueil\")'>$match->equipe</a>";
				} 
				else{
					$print = "<div onclick = 'match_detail(\"$match->no\",\"$match->equipe\",\"accueil\")'>";
					if($field == 'resultat') $print = $match->format_resultat();
					elseif($field == 'match'){

						if(stripos($match->local,"LUTRY") !== FALSE) $print .= "VBCLL-<span class = 'bold'>$match->visiteur</span>";
						else $print .= "<span class = 'bold'>$match->local</span>-VBCLL";

					}
					$print .= "</div>";
				}
				$row->cols[$field] = $print;
			}
			$table->rows[] = $row;
			$i++;
		}
		if($i){
			echo "<p class = 'bold'>Derniers r&eacute;sulats</p>\n";
			echo $table->print_header();
			echo $table->print_body();
			echo $table->print_footer();
		}

		$fields = array("equipe" => '',"match" => '',"date"  => 'center');
		$equipes = liste('equipes');
		$matchs = array();
		foreach($equipes as $id => $nom){
			${"match_".$id} = new match('next','',$nom);
			if(${"match_".$id}->no) $matchs[${"match_".$id}->date.${"match_".$id}->heure.${"match_".$id}->no] = ${"match_".$id};
		}
		// }
		ksort($matchs);
		$i = 0;

		$table = new table('prochains');
		foreach($fields as $field => $class){
			$$field = new table_field($field,'prochains');
			$$field->print = $field;
			$$field->styleclass = $class;
			$table->fields[] = $$field;
		}
		foreach($matchs as $key => $match){
			$equipe = new equipe($match->equipe,"nom");
			$equipe = new equipe($match->equipe,"nom");
			$row = new table_row();
			foreach($fields as $field => $class){
				if($field == 'equipe') $print = "<a href = 'javascript:equipes(\"$equipe->id\",\"resume\",\"accueil\")'>$match->equipe</a>";
				elseif($field == 'date'){
					$print = (ereg("[a-z]+",format_date($match->date,'d.m')) !== FALSE) ? "" : $match->jour;
					$print .= " ".format_date($match->date,'d.m');
					$print .= " ".format_heure($match->heure);
				} 
				elseif($field == 'match'){
					if(stripos($match->local,"LUTRY") !== FALSE) $print = "VBCLL-<span class = 'bold'>$match->visiteur</span>";
					else $print = "<span class = 'bold'>$match->local</span>-VBCLL";
					$print = "<div onclick = 'match_detail(\"$match->no\",\"$match->equipe\",\"accueil\")'>$print</div>";
				}
				$row->cols[$field] ="<div onclick = 'matchs(\"$match->no\")'>".$print."</div>";
			}
			$table->rows[] = $row;
			$i++;
		}
		if($i){
			echo "<p class = 'bold'>Prochains matchs</p>\n";
			echo $table->print_header();
			echo $table->print_body();
			echo $table->print_footer();
		}
		else echo "<p class = 'center bold'>Le championnat est termin&eacute;</p>\n";		
	}	
if(!$action){
	echo "</div>\n";
	pied();	
}
?>
